﻿using System;
using System.Collections.Generic;
using NCMIS.AccessControl;
using NCMIS.ObjectModel;
using NCMIS.ObjectModel.MetaData;
using NCMIS.Produce;
using NCMIS.Provider;
using System.Text;

namespace NCMIS.Provider.SampleProvider
{
    public class SampleObjectProvider : ObjectProviderBase
    {
        public SampleObjectProvider()
        {
            SampleFactory.Initialize();
        }

        public override CmisObject CreateDocument(string repositoryId, CmisProperties properties, string folderId, ContentStream contentStream, VersioningState versioningState, string[] policies, AccessControlList addACEs, AccessControlList removeACEs)
        {
            return SampleFactory.InsertDocument(properties, folderId, contentStream);
            //return SampleFactory.CreateCmisDocumentObject();
        }

        public override string CreateDocumentFromSource(string repositoryId, string sourceId, CmisProperties properties, string folderId, VersioningState? versioningState, string[] policies, AccessControlList addACEs, AccessControlList removeACEs)
        {
            return "Document 4711";
        }

        public override CmisObject CreateFolder(string repositoryId, CmisProperties properties, string folderId, string[] policies, AccessControlList addACEs, AccessControlList removeACEs)
        {
            return SampleFactory.InsertFolder(properties, folderId);
        }

        public override string CreateRelationship(string repositoryId, CmisProperties properties, string[] policies, AccessControlList addACEs, AccessControlList removeACEs)
        {
            return "Relationship 4711";
        }

        public override CmisObject CreatePolicy(string repositoryId, CmisProperties properties, string folderId, string[] policies, AccessControlList addACEs, AccessControlList removeACEs)
        {
            return SampleFactory.CreateCmisPolicyObject();
        }

        public override AllowableActions GetAllowableActions(string repositoryId, string objectId)
        {
            return SampleFactory.CreateAllowableActions();
        }

        public override CmisObject GetObject(string repositoryId, string objectId, string filter, bool includeAllowableActions, IncludeRelationships includeRelationships, string renditionFilter, bool includePolicyIds, bool includeAcl)
        {
            return SampleFactory.GetObject(objectId);
        }

        public override CmisProperties GetProperties(string repositoryId, string objectId, string filter)
        {
            return SampleFactory.CreateCmisProperties();
        }

        public override CmisObject GetObjectByPath(string repositoryId, string path, string filter, bool? includeAllowableActions, IncludeRelationships? includeRelationships, string renditionFilter, bool? includePolicyIds, bool? includeACL)
        {
            return SampleFactory.CreateCmisObject();
        }

        public override ContentStream GetContentStream(string repositoryId, string objectId, string streamId)
        {
            return SampleFactory.GetContentStream(objectId);

            //if (objectId == "calypsoo_logo")
            //    return SampleFactory.CreateImageContentStream();
            //return SampleFactory.CreateContentStream();
        }

        public override Renditions GetRenditions(string repositoryId, string objectId, string renditionFilter, string maxItems, string skipCount)
        {
            return new Renditions(new List<Rendition>(new Rendition[] { SampleFactory.CreateRendition() }));
        }

        public override TokenedCmisObjectId UpdateProperties(string repositoryId, string objectId, CmisProperties properties, string changeToken)
        {
            return SampleFactory.CreateTokenedCmisObjectId();
        }

        public override void MoveObject(string repositoryId, string objectId, string targetFolderId, string sourceFolderId)
        {
            return;
        }

        public override void DeleteObject(string repositoryId, string objectId, bool allVersions)
        {
            SampleFactory.DeleteObject(objectId);
            return;
        }

        public override string[] DeleteTree(string repositoryId, string folderId, bool? allVersions, UnfileObjects? unfileObject, bool? continueOnFailure)
        {
            return new string[] { "Object 17", "Object 4711" };
        }

        public override TokenedDocumentId SetContentStream(string repositoryId, string documentId, ContentStream contentStream, bool? overwriteFlag, string changeToken)
        {
            string decodedContent = ASCIIEncoding.UTF8.GetString(contentStream.Stream);

            // TODO: Save decoded content to file

            return SampleFactory.CreateTokenedDocumentId();
        }

        public override TokenedDocumentId DeleteContentStream(string repositoryId, string documentId, string changeToken)
        {
            return SampleFactory.CreateTokenedDocumentId();
        }
    }
}